package com.mouse.remind;

import java.lang.reflect.Field;
import java.util.Calendar;


import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.TimePicker;


/**
 * 修复bug的dialog类，提供与TimePickerDialog一样的方法
 * 
 * @version 1.0
 *
 */
public class FixedTimePickerDialog extends TimePickerDialog {

	
	public FixedTimePickerDialog(Context context, int theme,
			OnTimeSetListener callBack, int hourOfDay, int minute,
			boolean is24HourView) {
		super(context, theme, null, hourOfDay, minute, is24HourView);
		initializePicker(callBack);
	}

	public FixedTimePickerDialog(Context context, OnTimeSetListener callBack,
			int hourOfDay, int minute, boolean is24HourView) {
		super(context, null, hourOfDay, minute, is24HourView);
		initializePicker(callBack);
	}


	  private void initializePicker(final OnTimeSetListener callback) {
	    try {
	      //If you're only using Honeycomb+ then you can just call getDatePicker() instead of using reflection
	      Field pickerField = TimePickerDialog.class.getDeclaredField("mTimePicker");
	      pickerField.setAccessible(true);
	      final TimePicker picker = (TimePicker) pickerField.get(this);
	      this.setCancelable(true);
	      this.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getText(android.R.string.cancel), (OnClickListener) null);
	      this.setButton(DialogInterface.BUTTON_POSITIVE, getContext().getText(android.R.string.ok),
	          new DialogInterface.OnClickListener() {
	              @Override
	              public void onClick(DialogInterface dialog, int which) {
	                picker.clearFocus(); //Focus must be cleared so the value change listener is called
	                callback.onTimeSet(picker, picker.getCurrentHour(), picker.getCurrentMinute());
	              }
	          });
	    } catch (Exception e) { /* Reflection probably failed*/ }
	  }
	}